package com.morn.fanfou.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.morn.fanfou.exception.FanfouDBException;
import com.morn.fanfou.util.DateUtil;

public class Message implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7882914029552725464L;

	private String id;

	private String text;

	private String sender_id;

	private String recipient_id;

	private Date created_at;

	private String sender_screen_name;

	private String recipient_screen_name;

	private User sender;

	private User recipient;

	public Message() {

	}

	public Message(JSONObject json) throws FanfouDBException {
		init(json);
	}

	private void init(JSONObject json) throws FanfouDBException {
		if (json != null) {
			try {
				id = json.getString("id");
				text = json.getString("text");
				sender_id = json.getString("sender_id");
				recipient_id = json.getString("recipient_id");
				created_at = DateUtil.parseDate(json.getString("created_at"), "EEE MMM dd HH:mm:ss ZZZZZ yyyy");
				sender_screen_name = json.getString("sender_screen_name");
				recipient_screen_name = json.getString("recipient_screen_name");
				if (!json.isNull("sender")) {
					sender = new User(json.getJSONObject("sender"));
				}
				if (!json.isNull("recipient")) {
					recipient = new User(json.getJSONObject("recipient"));
				}
			} catch (JSONException e) {
				throw new FanfouDBException(e.getMessage() + ":" + json.toString(), e);
			}
		}
	}

	public static List<Message> getMessageList(JSONArray array) throws FanfouDBException, JSONException {
		int size = array.length();
		List<Message> list = new ArrayList<Message>();
		for (int i = 0; i < size; i++) {
			list.add(new Message(array.getJSONObject(i)));
		}
		return list;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getSender_id() {
		return sender_id;
	}

	public void setSender_id(String sender_id) {
		this.sender_id = sender_id;
	}

	public String getRecipient_id() {
		return recipient_id;
	}

	public void setRecipient_id(String recipient_id) {
		this.recipient_id = recipient_id;
	}

	public Date getCreated_at() {
		return created_at;
	}

	public void setCreated_at(Date created_at) {
		this.created_at = created_at;
	}

	public String getSender_screen_name() {
		return sender_screen_name;
	}

	public void setSender_screen_name(String sender_screen_name) {
		this.sender_screen_name = sender_screen_name;
	}

	public String getRecipient_screen_name() {
		return recipient_screen_name;
	}

	public void setRecipient_screen_name(String recipient_screen_name) {
		this.recipient_screen_name = recipient_screen_name;
	}

	public User getSender() {
		return sender;
	}

	public void setSender(User sender) {
		this.sender = sender;
	}

	public User getRecipient() {
		return recipient;
	}

	public void setRecipient(User recipient) {
		this.recipient = recipient;
	}

	@Override
	public String toString() {
		return "Message [id=" + id + ", text=" + text + ", sender_id=" + sender_id + ", recipient_id=" + recipient_id
				+ ", created_at=" + created_at + ", sender_screen_name=" + sender_screen_name
				+ ", recipient_screen_name=" + recipient_screen_name + ", sender=" + sender + ", recipient="
				+ recipient + "]";
	}

	public static class ComparatorMessage implements Comparator<Message> {

		@Override
		public int compare(Message object1, Message object2) {
			Date date1 = object1.getCreated_at();
			Date date2 = object2.getCreated_at();
			return date1.compareTo(date2);
		}

	}

}
